package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.activity.util.DateUtil;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.activity.SeckillGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * 秒杀商品的controller层
 */
@RestController
@RequestMapping("/api/seckillGoods")
public class ApiSeckillGoodsController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    /**
     * 获取单个商品
     * @param dateTime
     * @param SeckillGoodsId
     * @return
     */
    @GetMapping("/getSeckillGoods")
    public Result getSeckillGoods(String dateTime,String SeckillGoodsId){
        //1 校验参数
        if(StringUtils.isEmpty(dateTime) || StringUtils.isEmpty(SeckillGoodsId)){
            throw new GmallException("参数非法！",null);
        }

        //2 调用业务层方法
        SeckillGoods seckillGoods = seckillGoodsService.getSeckillGoods(dateTime,SeckillGoodsId);

        //3 响应数据
        return Result.ok(seckillGoods);
    }

    /**
     * 获取日期菜单
     * @return
     */
    @GetMapping("/getDateMenus")
    public Result getDateMenus(){
        return Result.ok(DateUtil.getDateMenus());
    }

    /**
     * 获取不同时间段的商品列表
     * @return
     */
    @GetMapping("/getSeckillGoodsList")
    public Result getSeckillGoodsList(String dateTime){
        //1 校验参数
        if(StringUtils.isEmpty(dateTime)){
            throw new GmallException("参数非法！",null);
        }

        //2 调用业务层方法
        List<SeckillGoods> seckillGoodsList = seckillGoodsService.getSeckillGoodsList(dateTime);

        //3 响应数据
        return Result.ok(seckillGoodsList);
    }
}
